import { login, login2, uploadUrl, visions} from '../../utils/api.js';
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'
Component({
  //组件的属性可以直接在组件标签上定义
  properties: {
    title:{
      type: String,
      value: '标题',
    },
    toHome:{
      type: Boolean,
      value: true,
    },
    content: {
      type: String,
      value: '此功能需要登录使用', 
    },
    confirm:{
      type: String,
      value: '确定',
    },
    cancel:{
      type: String,
      value: '取消',
    },
    show_loadnote:{
      type: Boolean,
      value: false,
    }
  },
  data: {// 私有数据，可用于模板渲染
    onload: 0,
    visionShow: 0,
    titlename: '详情',
    scienceid: '',
    avatarUrl: defaultAvatarUrl,
    avatar:'',
    nickName:'',
    usermessage: {
      userid: '',
      NickName: '',
      HeadUrl: '',
      openid: '',
      session_key: '',
    }
  },
  created(){
    visions()
  },
  attached(){
    this.setData({
      visionShow: wx.getStorageSync('visionShow')
    })
  },
  methods: {//方法   
    onChange(e){
      this.setData({
        nickName: e.detail
      })
    },
    onChooseAvatar(e) {   
      var that=this
      const { avatarUrl } = e.detail
      this.setData({
        avatarUrl,
      })
      wx.uploadFile({        
        filePath: avatarUrl,
        name: 'file',
        url: uploadUrl,
        formData:{
          "type": 'images'
        },
        success: function (res) {
          that.setData({
            avatar: JSON.parse(res.data).data.image_url[0]
          })
        },
        fail: function (res) {
          wx.showToast({
            title: JSON.parse(res.data).message,
            icon: 'none',
            duration: 3000
          })
        }
      })
    },
    close_reminder: function () {
      this.setData({
        show_loadnote: false
      })
      wx.showToast({
        title: '部分功能无法使用',
        icon: 'none',
        success:(res)=>{         
          if(this.data.toHome){
            wx.switchTab({
              url: '/pages/home/home'
            })
          }          
        }
      })
    },
    getUserProfile(e) {    
      let useInfo = ''
      if(this.data.visionShow){
        if (this.data.visionShow==2){
          let avatarUrl = this.data.avatar ? this.data.avatar: defaultAvatarUrl
          let nickName = this.data.nickName
          useInfo = {
            avatarUrl,
            nickName
          }
          if (!nickName) {
            wx.showToast({
              title: '请填写昵称',
              icon: 'none',
              duration: 3000
            })
            return;
          }
        }      
        login2(useInfo,this.data.toHome)
      }else{
        login(this.data.toHome)
      }
      
      // this.setData({
      //   show_loadnote: false
      // })
    },
  },
  lifetimes: {

  },
})